package org.htmlparser.parserapplications.filterbuilder.wrappers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.Set;
import javax.swing.JComboBox;
import org.htmlparser.Node;
import org.htmlparser.NodeFactory;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.PrototypicalNodeFactory;
import org.htmlparser.Tag;
import org.htmlparser.filters.NodeClassFilter;
import org.htmlparser.parserapplications.filterbuilder.Filter;

public class NodeClassFilterWrapper extends Filter
  implements ActionListener
{
  protected JComboBox mClass;
  protected NodeClassFilter mFilter = new NodeClassFilter();

  public NodeClassFilterWrapper()
  {
    this.mClass = new JComboBox();
    this.mClass.addItem("");
    add(this.mClass);
    this.mClass.addActionListener(this);
  }

  public boolean accept(Node paramNode)
  {
    return this.mFilter.accept(paramNode);
  }

  public void actionPerformed(ActionEvent paramActionEvent)
  {
    if (paramActionEvent.getSource() == this.mClass);
    try
    {
      this.mFilter.setMatchClass(Class.forName((String)this.mClass.getSelectedItem()));
      return;
    }
    catch (ClassNotFoundException localClassNotFoundException)
    {
      localClassNotFoundException.printStackTrace();
    }
  }

  public String getDescription()
  {
    return "Nodes of class";
  }

  public String getIconSpec()
  {
    return "images/NodeClassFilter.gif";
  }

  public NodeFilter getNodeFilter()
  {
    NodeClassFilter localNodeClassFilter = new NodeClassFilter();
    localNodeClassFilter.setMatchClass(this.mFilter.getMatchClass());
    return localNodeClassFilter;
  }

  public NodeFilter[] getSubNodeFilters()
  {
    return new NodeFilter[0];
  }

  public void setNodeFilter(NodeFilter paramNodeFilter, Parser paramParser)
  {
    this.mFilter = ((NodeClassFilter)paramNodeFilter);
    NodeFactory localNodeFactory = paramParser.getNodeFactory();
    if (localNodeFactory instanceof PrototypicalNodeFactory)
    {
      PrototypicalNodeFactory localPrototypicalNodeFactory = (PrototypicalNodeFactory)localNodeFactory;
      Iterator localIterator = localPrototypicalNodeFactory.getTagNames().iterator();
      while (localIterator.hasNext())
      {
        Tag localTag = localPrototypicalNodeFactory.get((String)localIterator.next());
        this.mClass.addItem(localTag.getClass().getName());
      }
    }
    this.mClass.setSelectedItem(this.mFilter.getMatchClass().getName());
  }

  public void setSubNodeFilters(NodeFilter[] paramArrayOfNodeFilter)
  {
  }

  public String toJavaCode(StringBuffer paramStringBuffer, int[] paramArrayOfInt)
  {
    StringBuffer localStringBuffer = new StringBuffer().append("filter");
    int i = paramArrayOfInt[1];
    paramArrayOfInt[1] = (i + 1);
    String str = i;
    spaces(paramStringBuffer, paramArrayOfInt[0]);
    paramStringBuffer.append("NodeClassFilter ");
    paramStringBuffer.append(str);
    paramStringBuffer.append(" = new NodeClassFilter ();");
    newline(paramStringBuffer);
    spaces(paramStringBuffer, paramArrayOfInt[0]);
    paramStringBuffer.append("try { ");
    paramStringBuffer.append(str);
    paramStringBuffer.append(".setMatchClass (Class.forName (\"");
    paramStringBuffer.append(this.mFilter.getMatchClass().getName());
    paramStringBuffer.append("\")); } catch (ClassNotFoundException cnfe) { cnfe.printStackTrace (); }");
    newline(paramStringBuffer);
    return str;
  }
}